/**
 * Copyright 2005 Darren L. Spurgeon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ce.cetag.tags.tabs;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
 * Tag handler for individual page within a AJAX tabbed panel.
 *
 * @author Darren Spurgeon
 * @version $Revision: 1.1 $ $Date: 2008/01/28 06:47:33 $ $Author: Code1_xuhaidui $
 */
public class AjaxTabPageTag extends TagSupport {


	private String tabId;

	private String caption;

	private String baseUrl;

	private String defaultTab;

	private String topDiv = "";

	private String rightDiv = "";

	private String bodyDiv = "";

	private String beforeFunction="";

	private String afterFunction="";

	private String canEdit="true";

	private String height="";

	private String jsParam="";

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getTabId() {
		return tabId;
	}

	public void setTabId(String tabId) {
		this.tabId = tabId;
	}

	public String getCanEdit() {
		return canEdit;
	}

	public void setCanEdit(String canEdit) {
		this.canEdit = canEdit;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public String getDefaultTab() {
		return defaultTab;
	}

	public void setDefaultTab(String defaultTab) {
		this.defaultTab = defaultTab;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getTopDiv() {
		return topDiv;
	}

	public void setTopDiv(String topDiv) {
		this.topDiv = topDiv;
	}
	public String getBodyDiv() {
		return bodyDiv;
	}

	public void setBodyDiv(String bodyDiv) {
		this.bodyDiv = bodyDiv;
	}

	public String getRightDiv() {
		return rightDiv;
	}

	public void setRightDiv(String rightDiv) {
		this.rightDiv = rightDiv;
	}

	public String getBeforeFunction() {
		return beforeFunction;
	}

	public void setBeforeFunction(String beforeFunction) {
		this.beforeFunction = beforeFunction;
	}

	public void setAfterFunction(String afterFunction){
		this.afterFunction=afterFunction;
	}

	public String getAfterFunction(){
		return afterFunction;
	}

	@Override
	public int doStartTag() throws JspException {

		this.tabId = (String) ExpressionEvaluatorManager.evaluate("tabId",
				this.tabId, String.class, this, super.pageContext);

		this.baseUrl = (String) ExpressionEvaluatorManager.evaluate("baseUrl",
				this.baseUrl, String.class, this, super.pageContext);

		this.caption = (String) ExpressionEvaluatorManager.evaluate("caption",
				this.caption, String.class, this, super.pageContext);

		if (this.defaultTab != null) {
			this.defaultTab = (String) ExpressionEvaluatorManager.evaluate(
					"defaultTab", this.defaultTab, String.class, this,
					super.pageContext);
		}

		if (this.canEdit != null) {
			this.canEdit = (String) ExpressionEvaluatorManager.evaluate(
					"canEdit", this.canEdit, String.class, this,
					super.pageContext);
		}

		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {

		Integer countTab=AjaxTabPanelTag.getCountTabs();
		StringBuffer script = new StringBuffer();

		script.append("<td><table id=\"tab_show_hide_"+this.tabId+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"hand\">");
		script.append("<tr id=\"onClick_"+this.tabId+"\" onClick=\"");
		script.append("if(isableTabEdit('"+this.tabId+"')){");
		if(!(beforeFunction.equals(""))){
			//script.append(beforeFunction);
			if(beforeFunction.indexOf(";")==-1){
				//script.append(";");
				beforeFunction=beforeFunction+";";
			}
			script.append("var tempFirstFunction="+beforeFunction.substring(0, beforeFunction.indexOf(";"))+";if(tempFirstFunction!=null &&(tempFirstFunction=='false' || !tempFirstFunction)){return false;}");
			script.append(beforeFunction.substring(beforeFunction.indexOf(";")+1, beforeFunction.length()));

		}
		script.append(""+AjaxTabPanelTag.getPanelID()+"showDiv(");
		script.append("'"+this.topDiv);
		script.append("', '");
		script.append(this.rightDiv);
		script.append("', '");
		script.append(this.baseUrl);
		script.append("', '");
		script.append(this.height);
		script.append("', '");
		script.append(this.jsParam);
		script.append("');sweattableDev('"+this.tabId+"',"+AjaxTabPanelTag.getPanelID()+"oldID);");
		script.append(AjaxTabPanelTag.getPanelID()+"oldID='"+this.tabId+"';");
		if(!(afterFunction.equals(""))){
			script.append(afterFunction);
			if(afterFunction.indexOf(";")==-1){
				script.append(";");
			}
	    }
		script.append("}\">");

        if("true".equals(this.canEdit)){
		    //关闭的样式start
		    script.append("<td class=\"tab_offLeftBg2\" id=\"tab_"+this.tabId+"1\"></td><td class=\"tab_offBg2\" id=\"tab_"+this.tabId+"2\">");
		    script.append(this.caption);
            script.append("</td><td class=\"tab_offRightBg2\" id=\"tab_"+this.tabId+"3\"></td></tr></table></td>");
            //关闭的样式end
        } else {
        	//不能编辑的样式
        	script.append("<td class=\"tab_huiLeftBg2\" id=\"tab_"+this.tabId+"1\"></td><td class=\"tab_huiBg2\" id=\"tab_"+this.tabId+"2\">");
		    script.append(this.caption);
            script.append("</td><td class=\"tab_huiRightBg2\" id=\"tab_"+this.tabId+"3\"></td></tr></table></td>");

        }

        script.append("<script type=\"text/javascript\">\n");
        if(!(this.topDiv.equals("")))
        script.append(" parentDiv_tag_top_"+AjaxTabPanelTag.getPanelID()+".appendChild(document.getElementById(\""+this.topDiv+"\"));\n");
        if(!(this.rightDiv.equals("")))
        script.append(" parentDiv_tag_right_"+AjaxTabPanelTag.getPanelID()+".appendChild(document.getElementById(\""+this.rightDiv+"\"));\n");
        script.append("</script>\n");



        if(countTab==1){

        	 StringBuffer scriptDefault = new StringBuffer();
        	 scriptDefault.append("<script type=\"text/javascript\">\n");
        	 if(!(beforeFunction.equals(""))){
        		 scriptDefault.append("try{");
     			scriptDefault.append(beforeFunction);
     			if(beforeFunction.lastIndexOf(";")+1!=beforeFunction.length()){
     				scriptDefault.append(";");
     			}
     			scriptDefault.append(" }catch(ex){}");
     		 }
        	 scriptDefault.append(AjaxTabPanelTag.getPanelID()+"oldID='"+this.tabId+"';");
        	 scriptDefault.append("defaultTabOldTop_ID_1='"+this.topDiv+"';");
    		 scriptDefault.append("defaultTabOldRight_ID_2='"+this.rightDiv+"';");
    		 scriptDefault.append("defaultTabOldBody_ID_3='"+this.baseUrl+"';");
    		 scriptDefault.append("defaultTabOldBody_height='"+this.height+"';");
    		 scriptDefault.append("defaultTabOldBody_jsParam='"+this.jsParam+"';");

    		scriptDefault.append("sweattableDev('"+this.tabId+"',"+AjaxTabPanelTag.getPanelID()+"oldID);\n");

    		scriptDefault.append(AjaxTabPanelTag.getPanelID()+"oldID='"+this.tabId+"';");

    		scriptDefault.append("</script>\n");

    		AjaxTabPanelTag.setDefaultTabStr(scriptDefault);
        }

        if (("true".equals(this.defaultTab))&& (!(countTab==1))) {

        	 StringBuffer scriptDefault = new StringBuffer();
        	 scriptDefault.append("<script type=\"text/javascript\">\n");
        	 if(!(beforeFunction.equals(""))){
        		 scriptDefault.append("try{");
      			scriptDefault.append(beforeFunction);
      			if(beforeFunction.lastIndexOf(";")+1!=beforeFunction.length()){
      				scriptDefault.append(";");
      			}
      			scriptDefault.append(" }catch(ex){}");
      		 }
    		scriptDefault.append(AjaxTabPanelTag.getPanelID()+"oldID='"+this.tabId+"';");
    		scriptDefault.append("defaultTabOldTop_ID_1='"+this.topDiv+"';");
    		scriptDefault.append("defaultTabOldRight_ID_2='"+this.rightDiv+"';");
    		scriptDefault.append("defaultTabOldBody_ID_3='"+this.baseUrl+"';");
    		scriptDefault.append("defaultTabOldBody_height='"+this.height+"';");
    		 scriptDefault.append("defaultTabOldBody_jsParam='"+this.jsParam+"';");

    		scriptDefault.append("sweattableDev('"+this.tabId+"',"+AjaxTabPanelTag.getPanelID()+"oldID);\n");

    		scriptDefault.append(AjaxTabPanelTag.getPanelID()+"oldID='"+this.tabId+"';");

    		scriptDefault.append("</script>\n");

    		AjaxTabPanelTag.setDefaultTabStr(scriptDefault);

    	}

		JspWriter writer = pageContext.getOut();
		try {
			writer.println(script);
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}

		return EVAL_PAGE;
	}


	@Override
	public void release() {
		this.tabId = null;
		this.caption = null;
		this.baseUrl = null;
		this.defaultTab = null;
		this.topDiv = null;
		this.rightDiv = null;
		this.bodyDiv = null;
		this.beforeFunction = null;
		this.afterFunction = null;
		this.canEdit = null;
		super.release();
	}

	public String getJsParam() {
		return jsParam;
	}

	public void setJsParam(String jsParam) {
		this.jsParam = jsParam;
	}




}
